package com.ssm.util.test.date;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import com.mysql.fabric.xmlrpc.base.Param;

public class DateUtil {

	// 时间转换
	public static void date() throws Exception {
		SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateStr = dateformat.format(System.currentTimeMillis());
		System.out.println("时间戳转日期：" + dateStr);
		// String format = dateformat.format(new Date());
		// System.out.println(format);
		// System.out.println(System.currentTimeMillis());
		long time = dateformat.parse("2016-09-02 23:02:17").getTime();
		System.out.println("日期转时间戳：" + time);
	}

	/**
	 * 得到今天
	 * 
	 * @return String
	 */
	public static String gettoday() {
		Calendar cal = Calendar.getInstance();
		return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
	}

	/**
	 * 得到上周最后一天
	 * 
	 * @return String
	 */
	public static String lastweekendDay() {
		Calendar cal = Calendar.getInstance();
		cal.setFirstDayOfWeek(Calendar.SUNDAY);// 将每周第一天设为星期一，默认是星期天
		cal.add(Calendar.DATE, -1 * 7);
		cal.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
		return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
	}

	/**
	 * 获得上周第一天
	 * 
	 * @return
	 */
	public static String lastweekstartDay() {
		Calendar cal = Calendar.getInstance();
		cal.setFirstDayOfWeek(Calendar.SUNDAY);// 将每周第一天设为星期一，默认是星期天
		cal.add(Calendar.DATE, -1 * 7);
		cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
		return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
	}

	/**
	 * 获取本月第一天
	 */
	public static String getMonthstartday() {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, 0);
		c.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
		return new SimpleDateFormat("yyyy-MM-dd ").format(c.getTime());
	}

	/**
	 * 获取本月最后一天
	 */
	public static String getMonthendday() {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
		return new SimpleDateFormat("yyyy-MM-dd ").format(c.getTime());
	}

	/**
	 * 获取上月第一天
	 */
	public static String getlastMonthendStartday() {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, -1);
		c.set(Calendar.DAY_OF_MONTH, 1);
		return new SimpleDateFormat("yyyy-MM-dd ").format(c.getTime());
	}

	/**
	 * 获取上月最后一天
	 */
	public static String getlastMonthendEndday() {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.DAY_OF_MONTH, 1);
		c.add(Calendar.DATE, -1);
		return new SimpleDateFormat("yyyy-MM-dd ").format(c.getTime());
	}

	/**
	 * 昨天
	 * 
	 * @return
	 */
	public static String getyesterday() {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, -1);
		return new SimpleDateFormat("yyyy-MM-dd ").format(cal.getTime());
	}

	/**
	 * 得到本周周一
	 * 
	 * @return yyyy-MM-dd
	 */
	public static String getMondayOfThisWeek() {
		Calendar c = Calendar.getInstance();
		int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
		if (day_of_week == 0)
			day_of_week = 7;
		c.add(Calendar.DATE, -day_of_week + 1);
		return new SimpleDateFormat("yyyy-MM-dd ").format(c.getTime());
	}

	/**
	 * 得到本周周日
	 * 
	 * @return yyyy-MM-dd
	 */
	public static String getSundayOfThisWeek() {
		Calendar c = Calendar.getInstance();
		int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
		if (day_of_week == 0)
			day_of_week = 7;
		c.add(Calendar.DATE, -day_of_week + 7);
		return new SimpleDateFormat("yyyy-MM-dd ").format(c.getTime());
	}
	
	public static void Param(){
		Calendar c = Calendar.getInstance();
		c.set(Calendar.DAY_OF_MONTH, 1);  //set 定位初始日期
		c.add(Calendar.DATE, -1);		  //add 修改天数，初始日期  的 日期运算
		c.get(Calendar.DAY_OF_WEEK);	  //get 获取c实例天数的具体指返回整形
		System.out.println(c.get(Calendar.DAY_OF_MONTH));
	} 

	public static void main(String[] args) throws Exception {
		//日期时间戳转换
		date();
		//获取今天日期
		System.out.println(gettoday());
		//获取上月最后一天
		System.out.println(getlastMonthendEndday());
		
		Param();
	}

}
